/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CRange.java
 *
 * Created on Jun 24, 2009, 8:33:38 AM
 */

package matpred.search;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import matpred.CalendarPanel;
import utilities.entities.SCalendar;

/**
 *
 * @author Demola
 */
public class CRange extends javax.swing.JPanel implements Search {

    /** Creates new form CRange */
    public CRange(String title, boolean show) {
        initComponents();

        this.title = title;
        dlm1 = new DefaultListModel();
        dlm2 = new DefaultListModel();
        jList1.setModel(dlm1);
        jList2.setModel(dlm2);
        jLabel1.setText(title);
        calList = CalendarPanel.getCalendarPanel().getCalendars();
        jList2.setVisible(show);
        jList2.setEnabled(show);
        loadCalendars();
    }

    private void loadCalendars(){
        dlm1.clear();
        dlm2.clear();
        Iterator<SCalendar> cal = calList.iterator();
        while(cal.hasNext()){
            SCalendar sc = cal.next();
            dlm1.addElement(sc);
            if(jList2.isEnabled()) dlm2.addElement(sc);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();

        jScrollPane1.setViewportView(jList1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("jLabel1");

        jScrollPane2.setViewportView(jList2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(203, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private String title;
    private String errorMsg;
    private DefaultListModel dlm1;
    private DefaultListModel dlm2;
    private ArrayList<SCalendar> calList;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    public Object getValues() {
        if(jList2.isVisible()){
            return new Object[]{ jList1.getSelectedValues(), jList2.getSelectedValues()};
        }else{
            return new Object[]{ jList1.getSelectedValues(), null};
        }
    }

    public void refreshUI(){
        calList = CalendarPanel.getCalendarPanel().getCalendars();
        loadCalendars();
    }

    public boolean checkValues() {
        int c = jList1.getSelectedIndex();
        if(c == -1){
            errorMsg = "No Calendar is selected for " + title;
            return false;
        }
        return true;
    }

    public String getErrorMessage() {
        return errorMsg;
    }

}
